package com.wondersgroup.retrofittest;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ScrollView;
import android.widget.Spinner;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.meetme.android.multistateview.MultiStateView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/1/24.
 */

public class ScrollActty extends AppCompatActivity {
    @Bind(R.id.spinner)
    Spinner spinner;
    @Bind(R.id.swipe_target)
    ScrollView swipeTarget;
    @Bind(R.id.swipe_layout)
    SwipeToLoadLayout swipeLayout;
    @Bind(R.id.msv_layout)
    MultiStateView msvLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scroll);
        ButterKnife.bind(this);
        initspinner();
        initSwipeLayout();
    }
    void initspinner(){
        String[] stateArr = {"empty", "loading", "neterr", "unkonwerr", "content"};

        SpinnerStringAdapter adapter = new SpinnerStringAdapter(this);
        adapter.setList(stateArr);
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        msvLayout.setState(MultiStateView.ContentState.EMPTY);
                        break;
                    case 1:
                        msvLayout.setState(MultiStateView.ContentState.LOADING);

                        break;
                    case 2:
                        msvLayout.setState(MultiStateView.ContentState.ERROR_NETWORK);

                        break;
                    case 3:
                        msvLayout.setState(MultiStateView.ContentState.ERROR_GENERAL);

                        break;
                    case 4:
                        msvLayout.setState(MultiStateView.ContentState.CONTENT);

                        break;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
    }
    void initSwipeLayout(){
        swipeLayout.setOnRefreshListener(refreshListener);
        swipeLayout.setOnLoadMoreListener(onLoadMoreListener);
    }
    OnRefreshListener refreshListener = new OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeLayout.setRefreshing(false);
                }
            },3000);
        }
    };

    OnLoadMoreListener onLoadMoreListener = new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            swipeLayout.postDelayed(new Runnable() {
                @Override
                public void run() {
                    swipeLayout.setLoadingMore(false);

                }
            },3000);
        }
    };
}
